﻿using PlatformNetCore.Common;
using PlatformNetCore.Entity;
using PlatformNetCore.IRepository;
using PlatformNetCore.IService;
using System;

namespace PlatformNetCore.Service
{
    public class CompanyService : ICompanyService
    {
        private readonly ICompanyRepository companyRepository;
        public CompanyService(ICompanyRepository _companyRepository)
        {
            companyRepository = _companyRepository;
        }

        #region 基本资料
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultResponse<Company> GetCompany()
        {
            ResultResponse<Company> resultResponse = new ResultResponse<Company>() { success = true, msg = CommonConst.Success };
            resultResponse.data = companyRepository.FirstOrDefault();
            if (resultResponse.data == null)
            {
                resultResponse.data = new Company();
            }
            return resultResponse;
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="entity"></param>
        public ResultResponse SaveCompany(Company model)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            if(string.IsNullOrWhiteSpace(model.ID))
            {
                model.ID = IdentityCreator.NextIdentity;
                model.AddDate = DateTime.Now;
                response.success = companyRepository.Add(model);
            }
            else
            {
                var o = companyRepository.FirstOrDefault();
                o.SiteName = model.SiteName;
                o.CompanyName = model.CompanyName;
                o.Email = model.Email;
                o.Address = model.Address;
                o.ContactPhone = model.ContactPhone;
                o.LogoImage = model.LogoImage;
                o.ServicePhone = model.ServicePhone;
                o.ServiceEmail = model.ServiceEmail;
                o.WebNumber = model.WebNumber;
                o.UpdateBy = model.UpdateBy;
                o.UpdateDate = DateTime.Now;
                response.success = companyRepository.Update(o);
            }
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }
        #endregion
    }
}
